﻿Imports MySql.Data.MySqlClient
Public Class frmadd_voyage
    Dim mysql As MySqlConnection = main_form.mysqlconection
    Dim mySqlCommand As New MySqlCommand
    Dim mySqlAdaptor As New MySqlDataAdapter
    Dim mySqlReader As MySqlDataReader
    Dim respone As Object
    Dim idvoys As String
    Dim idvoyn As String
   
    Private Sub frmadd_voyage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
        System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")

        mysql.Close()

        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If


        mySqlCommand.CommandText = "Select * from vesmain;"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader

            While (mySqlReader.Read())
                ComboBox1.Items.Add(mySqlReader("VSSNAM"))
                ComboBox2.Items.Add(mySqlReader("VSSNAM"))
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()

        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If


        mySqlCommand.CommandText = "SELECT * FROM voyage order by VOYAGEID DESC LIMIT 1;"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader


            If mySqlReader.HasRows.ToString = False Then

                txt_voyage.Text = "001"
            Else

                While (mySqlReader.Read())

                    txt_voyage.Text = Format(CInt(mySqlReader("VOYAGEID")) + 1, "000")

                End While

            End If

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()





    End Sub

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
        mysql.Close()
        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If
        respone = MsgBox("ยืนยันข้อมูลถูกต้อง", MsgBoxStyle.OkCancel + MsgBoxStyle.Information, "Warning Messsage")
        If respone = 1 Then
            Try
                mySqlCommand.Parameters.Clear()
                mySqlCommand.CommandText = "insert into voyage (VOYVESIDN, VOYVESIDS, VOYDATESN, VOYDATESS,VOYDATEEN,VOYDATEES,VOYVESNAMEN,VOYVESNAMES,VOYTIMEHHMMNN,VOYNAME) values (@voyvesidn,@voyvesids,@voydatesn,@voydatess,@voydateen,@voydatees,@voyvesnamen,@voyvesnames,@VOYTIMEHHMMNN,@VOYNAME)"
                mySqlCommand.Connection = mysql


                mySqlCommand.Parameters.AddWithValue("@voyvesidn", idvoyn)
                mySqlCommand.Parameters.AddWithValue("@voyvesids", idvoys)
                mySqlCommand.Parameters.AddWithValue("@voydatesn", date_eta_penang.Value.Date.ToString("dd-MM-yyyy"))
                mySqlCommand.Parameters.AddWithValue("@voydatess", date_krabi_eta.Value.Date.ToString("dd-MM-yyyy"))
                mySqlCommand.Parameters.AddWithValue("@voydateen", date_etd_penang.Value.Date.ToString("dd-MM-yyyy"))
                mySqlCommand.Parameters.AddWithValue("@voydatees", date_krabi_etd.Value.Date.ToString("dd-MM-yyyy"))
                mySqlCommand.Parameters.AddWithValue("@voyvesnamen", ComboBox2.Text)
                mySqlCommand.Parameters.AddWithValue("@voyvesnames", ComboBox1.Text)
                mySqlCommand.Parameters.AddWithValue("@VOYTIMEHHMMNN", DateTimePicker4.Text)
                mySqlCommand.Parameters.AddWithValue("@VOYNAME", txt_voyage.Text)
                mySqlCommand.ExecuteNonQuery()
                mysql.Close()

                MsgBox("Save Complete")
                Dim cf As New frmsearch_voyage

                cf.MdiParent = Me.MdiParent
                Me.Close()
                cf.Dock = DockStyle.Fill
                cf.Show()
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End If
        mysql.Close()




    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If

        mySqlCommand.CommandText = "Select * from vesmain where VSSNAM = '" & ComboBox2.Text & "';"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader

            While (mySqlReader.Read())
                idvoyn = mySqlReader("VESMAINID")
                txt_nationality.Text = mySqlReader("VSSNATION")
                txt_namemaster.Text = mySqlReader("VSSMASTER")
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()


    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If


        mySqlCommand.CommandText = "Select * from vesmain where VSSNAM = '" & ComboBox1.Text & "';"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader

            While (mySqlReader.Read())
                idvoys = mySqlReader("VESMAINID")
                txt_nationality2.Text = mySqlReader("VSSNATION")
                txt_namemaster2.Text = mySqlReader("VSSMASTER")
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()
    End Sub
End Class